const db = wx.cloud.database()
const timer = db.collection('timer')
var util = require('../../utils/util.js');
var begin = null
var str1 = []
var str2 = []
Page({
    /**
     * 页面的初始数据
     */
    data: {
        minHour: 0,
        maxHour: 23,
        showPop: false,
        timmer: [],
        currentDate: ''
    },

    /**
     * 自定义函数区
     */
    openPop(e) {
        this.setData({
            showPop: true,
            currentDate: util.formatTime(new Date())
        })
    },
    closePop(e) {
        this.setData({
            showPop: false
        })
    },
    cancelPick(e) {
        this.setData({
            showPop: false
        })
    },
    delete(e) {
        var that = this
        var index = e.currentTarget.dataset.index
        timer.doc(this.data.timmer[index]._id).remove({
            fail: function (err) {
                wx.showToast({
                    title: '没有数据辣~',
                    icon: 'error'
                })
            }
        })
        if (index != 0) {
            that.data.timmer.splice(index)
        }
        else {
            that.data.timmer[index].message = '~你还没有设置哦~'
            that.data.timmer[index].check = false
        }
        this.setData({
            timmer: that.data.timmer
        })
    },

    bindCountpp(e) {
        var that = this
        var termal = {
            message: '~你还没有设置哦~',
            check: false,
            _id: ''
        }                                        //获取数组对象，push入数组，便于后面的渲染
        that.data.timmer.push(termal)
        this.setData({
            timmer: that.data.timmer
        })
    },

    confirmSelection(e) {                        //闹钟响铃时间的确定
        var that = this
        var index = e.currentTarget.dataset.index
        var DateTime = e.detail
        that.data.timmer[index].message = e.detail
        this.setData({
            timmer: that.data.timmer,
            showPop: false
        })
        str2 = DateTime.split(':')
        if (this.data.timmer[index]._id === '') {
            timer.add({
                data: {
                    message: this.data.timmer[index].message,
                    check: this.data.timmer[index].check
                },
                success(res) {
                    that.data.timmer[index]._id = res._id
                }
            })
        }
        else {
            timer.doc(this.data.timmer[index]._id).set({
                data: {
                    message: this.data.timmer[index].message,
                    check: this.data.timmer[index].check
                }
            })
        }
        this.setData({
            timmer: that.data.timmer
        })
    },


    onChange(e) {                          //打开开关即触发函数，开始倒计时
        var that = this
        var index = e.currentTarget.dataset.index
        that.data.timmer[index].check = e.detail
        this.setData({
            timmer: that.data.timmer
        })
        // 此时that与this一致
        if (this.data.timmer[index].message != '~你还没有设置哦~') {
            timer.doc(this.data.timmer[index]._id).set({
                data: {
                    message: this.data.timmer[index].message,
                    check: this.data.timmer[index].check
                }
            })
        }
        if (e.detail == true) {
            var CurrentTime = util.formatTime(new Date())
            str1 = CurrentTime.split(':')
            // 相隔时间算法
            if (Number(str2[1]) >= Number(str1[1])) {
                if (((Number(str2[0]) <= 12) && (Number(str2[0]) >= 0)) && ((Number(str1[0]) <= 23) && (Number(str1[0]) >= 13))) {
                    this.setData({
                        hour: 24 - Number(str1[0]) + Number(str2[0]),
                        minute: Number(str2[1]) - Number(str1[1])
                    })
                }
                if (((Number(str2[0]) <= 23) && (Number(str2[0]) >= 13)) && ((Number(str1[0]) <= 12) && (Number(str1[0]) >= 0))) {
                    this.setData({
                        hour: Number(str2[0]) - Number(str1[0]),
                        minute: Number(str2[1]) - Number(str1[1])
                    })
                }
                else {
                    if (Number(str2[0]) >= Number(str1[0])) {
                        this.setData({
                            hour: Number(str2[0]) - Number(str1[0]),
                            minute: Number(str2[1]) - Number(str1[1])
                        })
                    }
                    else {
                        this.setData({
                            hour: 24 - Number(str1[0]) + Number(str2[0]),
                            minute: Number(str2[1]) - Number(str1[1])
                        })
                    }
                }
            }
            else {
                if (((Number(str2[0]) <= 12) && (Number(str2[0]) >= 0)) && ((Number(str1[0]) <= 23) && (Number(str1[0]) >= 13))) {
                    this.setData({
                        hour: 23 - Number(str1[0]) + Number(str2[0]),
                        minute: Number(str2[1]) - Number(str1[1]) + 60
                    })
                }
                if (((Number(str2[0]) <= 23) && (Number(str2[0]) >= 13)) && ((Number(str1[0]) <= 12) && (Number(str1[0]) >= 0))) {
                    this.setData({
                        hour: Number(str2[0]) - Number(str1[0]) - 1,
                        minute: Number(str2[1]) - Number(str1[1]) + 60
                    })
                }
                else {
                    if (Number(str2[0]) >= Number(str1[0])) {
                        this.setData({
                            hour: Number(str2[0]) - Number(str1[0]) - 1,
                            minute: Number(str2[1]) - Number(str1[1]) + 60
                        })
                    }
                    else {
                        this.setData({
                            hour: 23 - Number(str1[0]) + Number(str2[0]),
                            minute: Number(str2[1]) - Number(str1[1]) + 60
                        })
                    }
                }
            }
            if (begin) return
            begin = setInterval((function () {
                let hour = that.data.hour
                let minute = that.data.minute
                let second = that.data.second
                if (second == 0) {
                    if (minute == 0) {
                        if (hour == 0) {
                            return
                        } else {
                            hour -= 1
                            minute = 59
                            second = 59
                        }
                    } else {
                        minute -= 1
                        second = 59
                    }
                } else {
                    second -= 1
                }
                that.setData({
                    hour: hour,
                    minute: minute,
                    second: second
                })
                if (!(hour || minute || second)) {
                    wx.showModal({
                        content: '计时结束',
                        showCancel: false,
                    })
                    let audio = wx.createInnerAudioContext();
                    audio.src = '/aud/20220624_092748_1.m4a';
                    audio.play();
                    clearInterval(begin)
                    begin = null
                }
            }), 1000)
        }
        else {
            clearInterval(begin)
            begin = null
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        // 获取内容
        wx.cloud.callFunction({
            name: 'creater',
            success: function (res) {
                that.setData({
                    timmer: res.result.data
                })
                if (that.data.timmer.length == 0)//进入界面即开始调用
                {
                    var termal = {
                        message: '~你还没有设置哦~',
                        check: false,
                        _id: ''
                    }                                    //获取数组对象，push入数组，便于后面的渲染
                    that.data.timmer.push(termal)
                    that.setData({
                        timmer: that.data.timmer
                    })
                }
            }
        })

    },// 正确

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})